# -*- coding: utf-8 -*-
import numpy as np
from models.match import Match
from utils.database import SessionLocal
import math

class Strategy:
    """
    like Kaili Strategy
    """
    phi = (1 + math.sqrt(5)) / 2 - 1
    x1 = (1 - phi) * 10
    pjs = (phi * 10 - x1) / 5

    x_values = [x1, x1 + pjs, x1 + 2 * pjs, x1 + 3 * pjs, x1 + 4 * pjs, phi * 10, x1 + 6 * pjs, x1 + 7 * pjs,
                x1 + 8 * pjs, x1 + 9 * pjs, x1 + 10 * pjs]
    y_values = [1236, 1724, 2130, 2456, 2700, 2862, 2944, 2984, 3033, 3066, 3090]

    # 使用二次多项式拟合
    coefficients = np.polyfit(x_values, y_values, 2)
    a, b, c = coefficients

    @staticmethod
    def calculate_x(x):
        if x >11:
            x = 11
        return Strategy.a * x ** 2 + Strategy.b * x + Strategy.c

    # 如果原始资金为100美金，bs=1.5的情况下投注额是150美金,input=match.weight ,output=1.0~3.1
    @staticmethod
    def get_bs(weight):
        return round(Strategy.calculate_x(math.fabs(weight)*100), -1)/1000

if __name__ == "__main__":
    db = SessionLocal()
    matchs = db.query(Match).all()
    money = 0
    for match in matchs:
        print(match.league)
        #bs 位于1.0-3.0之间，如果原始资金为100美金，bs=1.5的情况下投注额是150美金
        bs = Strategy.get_bs(match.weight)
        print(bs)
        if match.performance=='Win':
            money+=bs
            #money+=2120
        if match.performance=='Lose':
            money -= bs
            #money -= 2120
    print(money)